#! /usr/bin/env python3
"""
Wrapper python script to launch the pretty_printer_test_py program
with the standard input set to the test file readtest_input.
"""

import subprocess
import os
import shlex
verbose = @VERBOSE@
test_script = r'@pretty_printer_test_py@'
test_args = @test_args@
gdb_path = '@gdb_path@'
args = [
    gdb_path,
    '-nx',
    '-batch',
    '-ex',
    f'source {test_script}',
    '-args',
    r'@pretty_printer_test_program@',
] + test_args


if verbose:
    print(f"Pretty printer test running command:\n{shlex.join(args)}")
# We assume we are running from project root, and require modules buried in the src directory
python_env = os.environ.copy()
python_env['PYTHONPATH'] = os.getcwd() + os.pathsep + python_env.get('PYTHONPATH', "")

proc = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
                      env=python_env)

if verbose or proc.returncode != 0:
    out = '\n'.join([f'[{os.path.basename(test_script)}] {line}' for line in proc.stdout.split('\n')])
    print(out)
else:
    print(f'[{os.path.basename(test_script)}] {"passed" if proc.returncode == 0 else "failed"}')
exit(proc.returncode)
